package com.example.jiaoan.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jiaoan.R;
import com.google.android.material.button.MaterialButton;

import java.util.List;
import com.example.jiaoan.model.BooksModel;

public class Books extends RecyclerView.Adapter<Books.holderView> {

    List<BooksModel> booksModels;

    itemListener mitemListener;

    public Books(List<BooksModel> booksModels,itemListener itemListener) {
        this.booksModels = booksModels;
        this.mitemListener = itemListener;
    }

    @NonNull
    @Override
    public holderView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
        return new holderView(view);
    }

    @Override
    public void onBindViewHolder(@NonNull holderView holder, @SuppressLint("RecyclerView") int position) {
        holder.item_title.setText(booksModels.get(position).getBook_name());
        holder.class_btn.setText(booksModels.get(position).getClass_name());
        holder.plan_btn.setText(booksModels.get(position).getNums());
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mitemListener.OnItemClick(booksModels.get(position).getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return this.booksModels.size();
    }

    public static class holderView extends RecyclerView.ViewHolder{

        TextView item_title;
        MaterialButton class_btn;
        MaterialButton plan_btn;

        CardView cardView;
        public holderView(@NonNull View itemView) {
            super(itemView);
            item_title = itemView.findViewById(R.id.item_title);
            class_btn = itemView.findViewById(R.id.class_btn);
            plan_btn = itemView.findViewById(R.id.plan_btn);
            cardView = itemView.findViewWithTag("card_tag");
        }
    }

    public interface itemListener{
        void OnItemClick(int id);
    }
}
